package com.sanpu.kxe.business.main.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.sanpu.kxe.R;
import com.sanpu.kxe.base.BaseTitleActivity;
import com.sanpu.kxe.business.main.bean.UserCenterOutput;
import com.sanpu.kxe.business.main.contract.MyQrcodeContract;
import com.sanpu.kxe.business.main.presenter.MyQrcodePresenter;
import com.sanpu.kxe.common.cache.CacheManage;
import com.sanpu.kxe.common.cache.CacheModel;
import com.sanpu.kxe.common.interfaces.UrlConstants;
import com.sanpu.kxe.common.utils.QRCode;
import com.sanpu.kxe.utils.ScreenUtil;
import com.sanpu.kxe.utils.ToastUtil;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Description：我的二维码
 * Created by 薛勇军 on 2018/8/24.
 */

public class MyQrcodeActivity extends BaseTitleActivity implements MyQrcodeContract.View {

    @Bind(R.id.qrcode)
    ImageView qrcode;
    @Bind(R.id.showLayout)
    LinearLayout showLayout;

    private MyQrcodeContract.Presenter mPresenter;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_my_qrcode;
    }

    @Override
    protected void initView() {
        super.initView();
        commonTitle.setTitle("我的二维码");
        mPresenter = new MyQrcodePresenter(this);
    }

    @Override
    protected void initData() {
        super.initData();
        UserCenterOutput retDatBean = CacheManage.getInstance().getCache(CacheModel.USER_CENTER_KEY);
        if (retDatBean != null) {
            String name = retDatBean.getUserName();
            String mCodeUrl = UrlConstants.INVITE_REG + name;
            Bitmap bitmap = QRCode.createQRCode(mCodeUrl, 500);
            if (bitmap == null) {
                ToastUtil.showTextViewPrompt("没有二维码");
            } else {
                qrcode.setImageBitmap(QRCode.resizeImage(bitmap, ScreenUtil.dp2px(210), ScreenUtil.dp2px(210)));
            }
        }
    }

    @OnClick({R.id.downloadRtv, R.id.shareRtv})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.downloadRtv:
                showLayout.setDrawingCacheEnabled(true);
                Bitmap saveBitmap = showLayout.getDrawingCache();
                if (saveBitmap != null) {
                    mPresenter.saveImage(saveBitmap);
                }
                break;
            case R.id.shareRtv:
                showLayout.setDrawingCacheEnabled(true);
                Bitmap shareBitmap = showLayout.getDrawingCache();
                if (shareBitmap != null) {
                    mPresenter.shareImage(shareBitmap);
                }
                break;
        }
    }

    @Override
    protected boolean hasBindEventBus() {
        return false;
    }

    @Override
    public void setPresenter(MyQrcodeContract.Presenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.onDestroyPresenter();
            mPresenter = null;
        }
    }
}
